package Util
{
	import data.AppConfig;
	
	import datastructure.Edge;
	import datastructure.HTNode;
	import datastructure.Vertex;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	import view.NodeCircle;

	public class DrawGraph
	{
		public function DrawGraph()
		{
			
		}
		
		/**
		 * 画顶点 
		 * @param vertexArray 顶点数组
		 * @param graphContainer	顶点容器
		 * 
		 */		
		public static function drawVertex(vertexArray:Array,graphContainer:Sprite):void
		{
			if(vertexArray.length == 0)
				return;
			
			for(var i:int = 0; i < vertexArray.length; i++)
			{
				var currentVert:Vertex = vertexArray[i] as Vertex;
				var nodeCircleSprit:NodeCircle;
				if(currentVert.isStart)
				{
					nodeCircleSprit = new NodeCircle(currentVert.name,AppConfig.BLACK);
				}
				else
				{
					nodeCircleSprit = new NodeCircle(currentVert.name);
				}
				
				nodeCircleSprit.x = currentVert.xCoordinate;
				nodeCircleSprit.y = currentVert.yCoordinate;
				graphContainer.addChild(nodeCircleSprit);
			}
		}
		
		/**
		 * 画图的所有边和权值 
		 * @param edgeArray	边的数组
		 * @param graphContainer 图的容器
		 * 
		 */		
		public static function drawLines(edgeArray:Array,graphContainer:Sprite):void
		{
			for(var i:int = 0;i < edgeArray.length; i++)
			{
				var edge:Edge = edgeArray[i] as Edge;
				graphContainer.addChildAt(drawLine(edge.startVer,edge.endVer,edge.weight),0);
			}
		}
		
		
		/**
		 * 画两个顶点之间的边和权值 
		 * @param v1
		 * @param v2
		 * @return Sprite对象
		 * 
		 */			
		private static function drawLine(v1:Vertex,v2:Vertex,weight:int):Sprite
		{
			var lineObject:Sprite = new Sprite();
			lineObject.graphics.clear();
			lineObject.graphics.lineStyle(2,0xff0000);
			lineObject.graphics.moveTo(v1.xCoordinate,v1.yCoordinate);
			lineObject.graphics.lineTo(v2.xCoordinate,v2.yCoordinate);
			
			var weightLabel:TextField = new TextField();
			weightLabel.text = weight.toString();
			weightLabel.x = (v1.xCoordinate + v2.xCoordinate)/2;
			weightLabel.y = (v1.yCoordinate + v2.yCoordinate)/2;
			
			lineObject.addChild(weightLabel);
			return lineObject;
		}
		
		
		public static function drawNodes(root:HTNode,treeContainer:Sprite):void
		{
			if(root == null)
				return;
			drawNodes(root.left,treeContainer);
			var nodeCircleSprit:NodeCircle = new NodeCircle(root.name);
			nodeCircleSprit.x = root.OffsetX;
			nodeCircleSprit.y = root.distance * 50;
			treeContainer.addChild(nodeCircleSprit);
			drawNodes(root.right,treeContainer);
		}
		
		public static function drawLinesTree(root:HTNode,treeContainer:Sprite):void
		{
			if(root == null)
				return;
			treeContainer.addChildAt(drawLineTree(root,root.parent),0);
			trace("draw:" + root.name);
			drawLinesTree(root.left,treeContainer);
			drawLinesTree(root.right,treeContainer);	
		}
		
		/**
		 * 画出两个节点之间的线 
		 * @param node
		 * @param parent
		 * @return 
		 * 
		 */		
		private static function drawLineTree(node:HTNode,parent:HTNode):Shape
		{
			if(parent == null)
				return new Shape();
			var lineObject:Shape = new Shape();
			lineObject.graphics.clear();
			lineObject.graphics.lineStyle(2,0xff0000);
			lineObject.graphics.moveTo(node.OffsetX,node.distance * 50);
			lineObject.graphics.lineTo(parent.OffsetX,parent.distance * 50);
			return lineObject;
		}
		
		
		
		
	}
}